<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>{{ floorName }}-楼层平面分区编辑</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="corner-box mb-10">
          <el-button type="primary" size="small" @click="editPoint(0)">添加本层平面分区</el-button>
        </div>
        <!--本体列表，数据 -->
        <div class="table-body">
          <div class="table-body-box">
            <el-table
                ref="multipleTable"
                :data="arryList"
                :border="tableoption.tableBorder"
                :height="tableHeight"
                size="mini"
                tooltip-effect="dark">
              <el-table-column
                  label="区域类型"
                  align="center"
                  width="120">
                <template slot-scope="scope">
<!--                  {{ Number(scope.row.type) == 1 ? '文本名称' : (Number(scope.row.type) == 2 ? '监控' : '区角') }}-->
                  {{areaList[scope.row.type]}}
                </template>
              </el-table-column>
              <el-table-column
                  label="名称"
                  align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.type==1">{{scope.row.data.name}}</span>
                  <span v-if="scope.row.type==2">{{scope.row.data.camera_data.pointername}}</span>
                  <span v-if="scope.row.type==3">{{scope.row.data.corner.name}}</span>
                  <span v-if="scope.row.type==4">{{scope.row.data.name}}</span>
                </template>
              </el-table-column>
              <el-table-column
                  label="操作"
                  width="180"
                  align="center">
                <template slot-scope="scope">
                  <el-button size="mini" @click="editPoint(scope.row.id)">编辑坐标</el-button>
                  <el-button size="mini" @click="deletePoint(scope.$index,scope.row.id)" type="danger" plain>删除
                  </el-button>
                  <!--                  <el-button size="mini" @click="editErroFn(scope.row.id)" v-if="Number(scope.row.type) == 2">事件历史-->
                  <!--                  </el-button>-->
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <!--工具条 分页-->
        <el-col :span="24" class="toolbarLine">
          <el-pagination
              background
              :page-size="page.pagesize"
              :current-page="page.pageindex"
              layout="total, prev, pager, next"
              :total="page.total"
              @current-change="handleSizeChange"></el-pagination>
        </el-col>
      </div>
      <div class="cornerComment-in-footer">
        <el-button type="primary" @click="closePOpup" size="small">确定</el-button>
      </div>
    </div>
    <pointEdit :visible.sync="pointVisible" :pointId="pointId" @InitFn="InitFn" :floorId="floorId"
               :mapPic="mapPic"></pointEdit>
    <erroHistory :visible.sync="erroVisible" :floorId="floorId"></erroHistory>
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js';
import pointEdit from "./pointEdit";
import erroHistory from "./erroHistory";

export default {
  props: ['visible', 'floorId', 'floorName', 'mapPic'], // commoent 学生点评 collection 学生采集
  components: {
    pointEdit,
    erroHistory
  },
  data() {
    return {
      dialogVisible: false,
      pointVisible: false,
      erroVisible: false,
      pointId: 0,
      tableoption: tableoption,
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      areaList:{
        1: '文本名称',
        2: '监控',
        3: '区角',
        4: '巡更',
      },
      arryList: [],
      tableHeight: 0
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        let n = this;
        this.$nextTick(() => {
          let height = n.$refs.dialogBox.offsetHeight;  //top 50 1 search 50 page 50 10 footer 50 1
          n.tableHeight = height - (51 + 43 + 50 + 20 + 51)
          console.log('height:', height)
        })
        this.InitFn()
      } else {
        this.arryList = []
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化
    async InitFn() {
      let option = {p: this.page.pageindex, map_id: this.floorId}
      let data = await this.$fetch(this.api.floor.pointIndex, option);
      console.log('data:', data)
      if (!data.code) {
        for (let item of data.list.data) {
          item.data = item.data ? JSON.parse(item.data) : ''
        }
        this.arryList = data.list.data
        this.page.total = data.list.total
      }
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    editPoint(id) {
      this.pointId = id
      this.pointVisible = true
    },
    // 删除点位
    deletePoint(index, id) {
      this.$confirm('确认永久删除地图坐标, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(index, id)
      }).catch(() => {
      })
    },
    detelRespon(index, id) {
      this.$post(this.api.floor.pointDelete, {id: id}).then((response) => {
        if (response.code === 0) {
          this.$message.success('删除成功!')
          this.InitFn()
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    editErroFn(id) {
      this.floorId = id
      this.erroVisible = true
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 70%;
  height: 60%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}
</style>


